package com.cq.service.impl;

import com.alibaba.excel.EasyExcel;
import com.cq.excelModel.ActivityGroupExcel;
import com.cq.mapper.ActivityGroupMapper;
import com.cq.pojo.ActivityGroup;
import com.cq.service.ActivityGroupService;
import com.cq.utils.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ActivityGroupServiceImpl implements ActivityGroupService {

    @Autowired
    private ActivityGroupMapper activityGroupMapper;

    @Override
    public List<ActivityGroup> findSplitActivity(String campaignName) {
        return activityGroupMapper.selectSplitActivity(campaignName);
    }

    @Override
    public void activityGroupDownload(HttpServletResponse response, String campaignName) throws IOException {
        List<ActivityGroup> list = activityGroupMapper.selectSplitActivity(campaignName);

        String fileName = System.currentTimeMillis() + "";

        List<ActivityGroupExcel> collect = list.stream()
                .map(a -> {
                    ActivityGroupExcel excel = new ActivityGroupExcel();
                    BeanUtils.copyProperties(a, excel);
                    return excel;
                })
                .collect(Collectors.toList());

        EasyExcel.write(response.getOutputStream(), ActivityGroupExcel.class)
                .registerWriteHandler(ExcelUtil.getHorizontalCellStyleStrategy(response, fileName))
                .sheet("分活动")
                .doWrite(collect);
    }
}
